<?php defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * Description: 一些能用方法
 *
 * @author      Eden
 * @datetime    2016/12/10 17:09
 * @copyright   Beijing CmsTop Technology Co.,Ltd.
 */

class Common_Model extends ADM_Model
{
    /**
     * 初始化
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 获取物流公司信息
     * @return bool
     */
    public function getExpress()
    {
        try {
            $query = $this->db->select('id, simpleName, phone, expName, url, imgUrl')
                ->where(array('status' => 1))
                ->order_by('sort', 'DESC')
                ->get('system_express');
            return $query->result_array();
        }catch (PDOException $e) {
            return false;
        }
    }

    /**
     * 获取所有分类
     * @return array|bool
     */
    public function getAllGoodsCategories()
    {
        $rows = array();
        try {

            $q = $this->db->select('id, parentId, name, sort')
                ->where(array('status' => 1))
                ->order_by('sort', 'DESC')
                ->get('base_category');

            $rows = $q->result_array();
            if (!empty($rows)) foreach($rows as &$row) {
                $row['id'] = intval($row['id']);
                $row['parentId'] = intval($row['parentId']);
                $row['sort'] = intval($row['sort']);
            }
        } catch(PDOException $e) {
            return false;
        }

        return $rows;
    }

    /**
     * 获取单位
     */
    public function getUnits()
    {
        $q = $this->db->get_where('base_unit');
        $rows = $q->result_array();
        return !empty($rows) ? $rows : [];
    }

    /**
     * 获取指定字段的基地信息
     * @param string $fields
     * @return array
     */
    public function getAllBaseName($fields = '')
    {
        $fields = !empty($fields) ? $fields : '*';
        $q = $this->db->select($fields)
            ->from('base')
            ->where(array('status' => 1))
            ->get();
        $rows = $q->result_array();
        if (!empty($rows)) foreach ($rows as &$row) {
            $row['id'] = intval($row['id']);
        }
        return !empty($rows) ? $rows : [];

    }

}

